package com.goods.business.controller;

import com.goods.business.service.InStockService;
import com.goods.common.error.SystemException;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.InStockDetailVO;
import com.goods.common.vo.business.InStockVO;
import com.goods.common.vo.system.PageVO;
import com.goods.common.vo.system.UserInfoVO;
import com.goods.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author liangliang
 * @Date 2021/11/8 13:16 Administrator
 * @Description
 */
@RestController
@RequestMapping("/business/inStock")
public class InStockController {
    @Autowired
    private InStockService inStockService;
    @Autowired
     private UserService userService;

    /**
     * 分页查询入库记录
     * @param
     * @param
     * @param
     * @return
     */
    @GetMapping("/findInStockList")
    public ResponseBean findInStockList(@RequestParam Map map){

     PageVO<InStockVO> inStockVOPageVO =inStockService.findInStockList(map);
    return ResponseBean.success(inStockVOPageVO);
    }

    /**
     * 查询入库明细
     * @param id
     * @param pageNum
     * @param
     * @return
     */
    @GetMapping("/detail/{id}")
    public ResponseBean detail(@PathVariable  Long id,
                               @RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum
                              ){
   InStockDetailVO inStockDetailVO =inStockService.findDetail(id,pageNum);
        return ResponseBean.success(inStockDetailVO);
    }

    /**
     * 将已经入库的移入回收站
     * @param id
     * @return
     */
    @PutMapping("/remove/{id}")
    public ResponseBean remove(@PathVariable Long id){
        inStockService.remove(id);
        return ResponseBean.success();
    }

    /**
     * 还原回收站中的数据
     * @param id
     * @return
     */
    @PutMapping("/back/{id}")
    public ResponseBean back(@PathVariable Long id){
    inStockService.back(id);
    return ResponseBean.success();
    }

    /**
     * 删除回收站中的数据
     * @param id
     * @return
     */
    @GetMapping("delete/{id}")
    public ResponseBean delete(@PathVariable Long id){
        inStockService.delete(id);
        return ResponseBean.success();
    }


    /**
     * 审核通过
     * @param id
     * @return
     */
    @PutMapping ("publish/{id}")
    public ResponseBean publish(@PathVariable Long id){
        inStockService.publish(id);
        return ResponseBean.success();
    }

    /**
     * 添加库存
     * @return
     * @param inStockVO
     */
    @PostMapping("/addIntoStock")
        public ResponseBean addIntoStock(@RequestBody InStockVO inStockVO){
        try {
            UserInfoVO userInfoVO = userService.info();
            String username = userInfoVO.getUsername();
            inStockVO.setOperator(username);
        } catch (SystemException e) {
            e.printStackTrace();
        }
        inStockService.addIntoStock(inStockVO);

            return ResponseBean.success();
        }
}
